package com.shlg.chuang.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.shlg.chuang.domain.Content;
import com.shlg.chuang.domain.ContextType;
import com.shlg.chuang.domain.ShowUser;
import com.shlg.chuang.service.ContentService;
import com.shlg.chuang.service.ShowUserService;

@RestController
@RequestMapping("/contents")
public class MainBizController {

	@Autowired
	ContentService contentService;
	
	@Autowired
	ShowUserService showUserService;
	
	@RequestMapping(value = "upContent", method = RequestMethod.POST)
	public Content upContent(@RequestBody Content content) {
		
		if(content.getContext().contains("#学习")){
			content.setContextType(ContextType.STUDY);
		} else if(content.getContext().contains("#帮助")){
			content.setContextType(ContextType.SUPPORT);
		} else if(content.getContext().contains("#分享")){
			content.setContextType(ContextType.SHARE);
		} else if(content.getContext().contains("#社区")){
			content.setContextType(ContextType.COMMUNITY);
		} else if(content.getContext().contains("#建议")){
			content.setContextType(ContextType.ADVISE);
		} else if(content.getContext().contains("#咨询")){
			content.setContextType(ContextType.CONSULT);
		} else {
			content.setContextType(ContextType.OTHER);
		} 
		Date upDateDate = new Date();
		String upDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(upDateDate);
		content.setUpTime(upDateString);
		return contentService.saveContent(content);
	}
	
	@RequestMapping(value = "searchScope", method = RequestMethod.GET)
	public List<Content> searchScope(@Param(value = "latitude") Double latitude, 
			@Param(value = "longitude") Double longitude, 
			@Param(value = "showUserId") Long showUserId) {
		
		ShowUser showUser = showUserService.findById(showUserId);
		return contentService
				.searchContentWhereLocationLimitInScope(latitude, longitude,
						showUser.getScope());
	}
	
	@RequestMapping(value = "filterType", method = RequestMethod.GET)
	public List<Content> filterType(@Param(value = "contextType") int contextType,
			@Param(value = "latitude") Double latitude, 
			@Param(value = "longitude") Double longitude, 
			@Param(value = "showUserId") Long showUserId) {
		
		ShowUser showUser = showUserService.findById(showUserId);
		ContextType contextType2;
		switch (contextType) {
			case 0:
				contextType2 = ContextType.STUDY;
				break;
			case 1:
				contextType2 = ContextType.SUPPORT;
				break;
			case 2:
				contextType2 = ContextType.SHARE;
				break;
			case 3:
				contextType2 = ContextType.COMMUNITY;
				break;
			case 4:
				contextType2 = ContextType.ADVISE;
				break;
			case 5:
				contextType2 = ContextType.CONSULT;
				break;
			default:
				contextType2 = ContextType.OTHER;
				break;
		}
		return contentService
				.filterByContentType(contextType2, latitude, longitude, showUser.getScope());
	}
	
	
	@RequestMapping(value = "getUserMessageFromContent", method = RequestMethod.GET)
	public ShowUser getUserMessageFromContent(@Param(value = "contentId") Long contentId) {
		Content content = contentService.findOne(contentId);
		return showUserService.findById(content.getShowUserId());
	}
	
	
	@RequestMapping(value = "findUserContent", method = RequestMethod.GET)
	public List<Content> findUserContent(@Param(value = "showUserId") Long showUserId) {
		return contentService.findContentByUserId(showUserId);
	}
	
	@RequestMapping(value = "findOne", method = RequestMethod.GET)
	public Content findOne(@Param(value = "contentId") Long contentId) {
		return contentService.findOne(contentId);
	}
	
	@RequestMapping(value = "deleteOne", method = RequestMethod.GET)
	public String deleteOne(@Param(value = "contentId") Long contentId){
		contentService.deleteContent(contentId);
		return "Your selected is deleted";
	}
		
}
